{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = os.getcwd()\n",
    "file_list = ['train.csv', 'val.csv']\n",
    "image_source_dir = os.path.join(root_dir, 'data/images/')\n",
    "data_root = os.path.join(root_dir, 'data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for file in file_list:\n",
    "    \n",
    "    image_target_dir = os.path.join(data_root, file.split(\".\")[0])\n",
    "    \n",
    "    # read list of image files to process from file\n",
    "    image_list = pd.read_csv(os.path.join(data_root, file), header=None)[0]\n",
    "    \n",
    "    print(\"Start preprocessing images\")\n",
    "    for image in image_list:\n",
    "        # open image file\n",
    "        img = cv2.imread(os.path.join(image_source_dir, image))\n",
    "        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "        \n",
    "        # perform transformations on image\n",
    "        b = cv2.distanceTransform(img, distanceType=cv2.DIST_L2, maskSize=5)\n",
    "        g = cv2.distanceTransform(img, distanceType=cv2.DIST_L1, maskSize=5)\n",
    "        r = cv2.distanceTransform(img, distanceType=cv2.DIST_C, maskSize=5)\n",
    "        \n",
    "        # merge the transformed channels back to an image\n",
    "        transformed_image = cv2.merge((b, g, r))\n",
    "        target_file = os.path.join(image_target_dir, image)\n",
    "        print(\"Writing target file {}\".format(target_file))\n",
    "        cv2.imwrite(target_file, transformed_image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
